package src.Utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;


import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class SQLPool {
    private static DataSource dataSource =  null;
    private Properties properties = null;
    private static final SQLPool instance = new SQLPool();

    {
        properties = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream("src/main/druid.properties"));
            properties.load(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws Exception {
        //通过druid工厂创建数据源
        dataSource = DruidDataSourceFactory.createDataSource(properties);
        return dataSource.getConnection();
    }


    public static SQLPool getInstance() {
        return instance;
    }

    @Test
    public void test() throws Exception {
        Statement st = getConnection().createStatement();
        PreparedStatement preparedStatement = getConnection().prepareStatement("select * from user");
        ResultSet rs = preparedStatement.executeQuery("select * from user");
        while (rs.next()){
           System.out.println(rs.getInt(1));
        }
    }


}
